package controllers
{
	import models.vo.RoomVO;
	import models.vo.UserVO;
	
	import mx.collections.ArrayCollection;

	public class RoomController
	{
		[Bindable]
		public var rooms:ArrayCollection = new ArrayCollection();
		public var roomMap:Object = new Object();
		public var playerNumMax:int = 2;
		public var playerNumMin:int = 2;
		
		[DexterEvent]
		public function createRoom(flag:int = 0):void{
			var user:UserVO = UserVO.self;
//			user.room = user.id;
//			user.part = flag;
			sendDexterEvent("broadcast","createRoom",user.id,flag);
		}
		[DexterEvent]
		public function $createRoom(userId:String,flag:int):void{
			var userVO:UserVO = sendDexterEvent("getUserByID",userId) as UserVO;
			userVO.part = flag;
			userVO.room = userId;
			var room:RoomVO = new RoomVO(playerNumMin,playerNumMax);
			room.creator = userVO;
			room.setItemAt(userVO,flag);
			rooms.addItem(room);
			roomMap[userId] = room;
		}
		[DexterEvent]
		public function joinRoom(room:String,part:int):void{
			//roomMap[room].setItemAt(UserVO.self,part);
			UserVO.self.part = part;
			UserVO.self.room = room;
			sendDexterEvent("joinOtherRoom",roomMap[room]);
			sendDexterEvent("sendToOthers","joinRoom",UserVO.self.id,room,part);
		}
		[DexterEvent]
		public function $joinRoom(userId:String,room:String,part:int):void{
			var userVO:UserVO = sendDexterEvent("getUserByID",userId) as UserVO;
			roomMap[room].setItemAt(userVO,part);
			userVO.part = part;
			userVO.room = room;
			if(UserVO.self.room == room){
				sendDexterEvent("joinMyRoom",userVO);
			}
		}
		[DexterEvent]
		public function exitRoom():void{
			var user:UserVO = UserVO.self;
			if(user.isCreator) removeRoom(user.room);
			user.quitRoom();
			sendDexterEvent("clearRoom");
			sendDexterEvent("sendToOthers","exitRoom",UserVO.self.id);
		}
		[DexterEvent]
		public function $exitRoom(id:String):void{
			var user:UserVO = sendDexterEvent("getUserByID",id) as UserVO;
			userOffline(user);
		}
		[DexterEvent]
		public function alreadyOnline(userVO:UserVO):void{
			if(!userVO.room)return;
			var room:RoomVO = getRoom(userVO.room);
			if(!room){
				room = new RoomVO(playerNumMin,playerNumMax);
				room.setItemAt(userVO,userVO.part);
				if(userVO.isCreator) room.creator = userVO;
				rooms.addItem(room);
				roomMap[userVO.room] = room;
			}else{
				room.setItemAt(userVO,userVO.part);
				if(userVO.isCreator) room.creator = userVO;
			}
		}
		[DexterEvent]
		public function userOffline(user:UserVO):void{
			if(!user.room)return;
			var room:RoomVO = getRoom(user.room);
			if(user.isCreator){
				if(UserVO.self.room == user.room){
					UserVO.self.quitRoom();
					sendDexterEvent("clearRoom");
				}
				removeRoom(user.room);
			}else{
				if(UserVO.self.room == user.room){
					sendDexterEvent("leaveMyRoom",user);
				}
			}
			user.quitRoom();
		}
		[DexterEvent]
		public function getRoom(room:String):RoomVO{
			return roomMap[room];
		}
		[DexterEvent]
		public function removeRoom(room:String):void{
			rooms.removeItemAt(rooms.getItemIndex(roomMap[room]));
			delete roomMap[room];
		}
		[DexterEvent]
		public function canStart():Boolean{
			return getRoom(UserVO.self.room).canStart();
		}
	}
}